package com.teyao.teyao_back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.teyao.teyao_back.entity.ClientInfo;
import com.teyao.teyao_back.entity.ResponseMessage;
import com.teyao.teyao_back.service.ClientInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/clientInfo")
public class ClientInfoController {
    @Autowired
    private ClientInfoService clientInfoService;

    //查询是否存在个人信息
    @GetMapping("/exists")
    public ResponseMessage exists(Integer userId) {
        return clientInfoService.myExists(userId);
    }

    //保存个人信息
    @PostMapping("/saveInfo")
    public ResponseMessage saveInfo(ClientInfo clientInfo) {
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setSuccess(clientInfoService.save(clientInfo));
        return responseMessage;
    }

    //获取个人信息
    @GetMapping("/getInfo")
    public ResponseMessage getInfo(Integer userId) {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<ClientInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        responseMessage.setData(clientInfoService.getOne(queryWrapper));
        return responseMessage;
    }

    //修改个人信息
    @GetMapping("/updateInfo")
    public ResponseMessage updateInfo(ClientInfo clientInfo) {
        ResponseMessage responseMessage = new ResponseMessage();
        UpdateWrapper<ClientInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", clientInfo.getUserId());
        boolean success = clientInfoService.update(clientInfo, updateWrapper);
        responseMessage.setSuccess(success);
        return responseMessage;
    }
}
